    
    
    const Koa = require('koa');
    const KoaRouter = require('koa-router');
    // koa-bodyparser模块用于存储页面发来的请求数据 存储格式为json对象
    const bodyParser = require('koa-bodyparser');
    const koaNunjucks = require('koa-nunjucks-2');
    const koaSession = require('koa-session');
    const path = require('path');
    const fs = require('fs');
    // let documentRoot = 'D:/web/apache/htdocs/tp5';
    let documentRoot = 'C:/Users/zhai/Desktop/实验项目';

    const app = new Koa();
    const router = new KoaRouter();

    app.keys = ["some secret hurr"];

    app.use(bodyParser());
    app.use(koaNunjucks({
        ext: 'html',
        path: path.join(__dirname, 'views'),
        nunjucksConfig: {
            trimBlocks: true
        }
    }));
    app.use(koaSession(app));
    app.use(router.routes());
    app.use(router.allowedMethods());

    app.listen('3000', (err) => {
        if(err) console.log("koa 服务器启动失败");
        else console.log("koa 服务器启动成功，地址为http://localhost:3000");
    });

    router.get('/', async (ctx, next) => {
        await ctx.render('home', {});
    });

    router.get('/login', async (ctx, next) => {
        console.log(ctx.url);
        ctx.request.url = '/userdata';
        console.log(ctx.url);
        await ctx.render('userdata', {
            username: 'tom'
        });
    });

    router.get('/userdata', async (ctx, next) => {
        let username = 'jack';
        await ctx.render('userdata', {
            username: username
        });
    });

    router.get('/filesystem', async (ctx, next) => {
        let currentroot = "";
        let resinfo = {};
        // 对请求字符串进行处理 获取请求数据
        // ctx.request.url 可以获取当前路由下的请求
        // ctx.request.url= 可以重写路由
        if(ctx.request.url.indexOf("?")!=-1) {
            let resarr1 = ctx.request.url.split("?")[1];
            let resarr2 = resarr1.split("&");
            for(let i=0;i<resarr2.length;i++) {
                let info = resarr2[i].split("=");
                // 对象创建属性和赋值
                resinfo[info[0]] = info[1];
            }
            currentroot = resinfo.reqFileurl;
            // 获取数据后 重写url 重新渲染页面
            ctx.request.url = '/filesystem';
        }
        let fileData = await new Promise(resolve => {
            // 存储当前页面的文件信息
            let fileData = {
                upPath: '',
                currentPath: '',
                nextPath: [],
                filesname: [],
                isDir: []
            }
            fs.readdir(`${documentRoot}${currentroot}`, (err, files) => {
                if(err) ctx.body = '读取错误';
                fileData.upPath = resinfo.upPath;
                fileData.currentPath = currentroot;
                for(let i=0;i<files.length;i++) {
                    fileData.filesname.push(files[i]);
                    fileData.nextPath.push(`${currentroot}/${files[i]}`);
                    if(fs.statSync(`${documentRoot}${fileData.nextPath[i]}`).isDirectory()) fileData.isDir.push(true);
                    else fileData.isDir.push(false);
                }
                resolve(fileData);
            });
        });
        console.log(fileData);
        await ctx.render('filesystem', {
            fileData: fileData
        });
    });
    